package com.cadre.web.controller.officer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.enums.ConferenceStateEnum;
import com.cadre.common.utils.R;
import com.cadre.common.utils.SecurityUtils;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.system.domain.officer.*;
import com.cadre.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Api(tags = {"上会管理-上报名单管理"})
@RequestMapping({"/profess/confernamelist"})
@RestController
/* loaded from: ConfernamelistController.class */
public class ConfernamelistController {
    @Autowired
    private ConfernamelistService confernamelistService;
    @Autowired
    private BatchconferenceService batchconferenceService;
    @Autowired
    private A01Service a01Service;
    @Resource
    private A19Service a19Service;
    @Autowired
    private PostchangeService postchangeService;

    @PostMapping({"/getListByBatchId"})
    @ApiOperation("获取批次人员名单列表")
    public R getListByBatchId(@RequestParam Map<String, Object> param) {
        if (param.get("submissdept") == null) {
            param.put("submissdept", SecurityUtils.getUserId());
        }
        if (param.get("submissdept").equals("admin")) {
            param.remove("submissdept");
        }
        return R.ok().put("list", (Object) this.confernamelistService.getListByBatchId(param));
    }

    @RequestMapping({"/info/{flowid}"})
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("confernamelist", (Object) ((ConfernamelistEntity) this.confernamelistService.getBaseMapper().selectById(flowid)));
    }

    @PostMapping({"/saveList/{batchId}"})
    @Transactional
    @ApiOperation("添加人员---确定")
    public R saveList(@PathVariable("batchId") String batchId, @RequestBody ConfernamelistEntity[] confernamelistEntity_arr) {
        for (ConfernamelistEntity c : confernamelistEntity_arr) {
            c.setSubmissdept(SecurityUtils.getDeptId());
            c.setResult(ConferenceStateEnum.NOT_SUBMITED.getValue());
            if (this.confernamelistService.getBaseMapper().selectList(new LambdaQueryWrapper<ConfernamelistEntity>().eq(ConfernamelistEntity::getBatchid, batchId).eq(ConfernamelistEntity::getPersonid, c.getPersonid())).size() == 0) {
                this.confernamelistService.getBaseMapper().insert(c);
                Map<String, Object> map = new HashMap<>();
                map.put("personid", c.getPersonid());
                map.put("a1935", 1);
                for (A19 a19 : this.a19Service.selectByList(map)) {
                    PostchangeEntity p = setPostChange(a19);
                    p.setNamelistid(batchId);
                    this.postchangeService.getBaseMapper().insert(p);
                }
            }
        }
        return R.ok();
    }

    @PostMapping({"/saveListForyichu/{batchId}"})
    @Transactional
    public R saveListForyichu(@PathVariable("batchId") String batchId, @RequestBody ConfernamelistEntity[] confernamelistEntity_arr) {
        for (ConfernamelistEntity c : confernamelistEntity_arr) {
            c.setSubmissdept(SecurityUtils.getDeptId());
            c.setResult(ConferenceStateEnum.SUBMITED.getValue());
            List<ConfernamelistEntity> list = this.confernamelistService.getBaseMapper().selectList(
                    new LambdaQueryWrapper<ConfernamelistEntity>().eq(ConfernamelistEntity::getBatchid, batchId).eq(ConfernamelistEntity::getPersonid, c.getPersonid()));
            if (list.size() > 0) {
                return R.error("error");
            }
            if (list.size() == 0) {
                this.confernamelistService.getBaseMapper().insert(c);
                Map<String, Object> map = new HashMap<>();
                map.put("personid", c.getPersonid());
                map.put("a1935", 1);
                for (A19 a19 : this.a19Service.selectByList(map)) {
                    PostchangeEntity p = setPostChange(a19);
                    p.setNamelistid(batchId);
                    this.postchangeService.getBaseMapper().insert(p);
                }
            }
        }
        return R.ok();
    }

    private PostchangeEntity setPostChange(A19 a19) {
        PostchangeEntity p = new PostchangeEntity();
        p.setA19flowid(a19.getFlowid());
        p.setA1905(a19.getA1905());
        p.setA1910(a19.getA1910());
        p.setA1915(a19.getA1915());
        p.setA1916(a19.getA1916());
        p.setA1920(a19.getA1920());
        p.setA1925(a19.getA1925());
        p.setA1930(a19.getA1930());
        p.setA1935(a19.getA1935());
        p.setA1940(a19.getA1940());
        p.setA1945(a19.getA1945());
        p.setA1950(a19.getA1950());
        p.setA1955(a19.getA1955());
        p.setA1956(a19.getA1956());
        p.setA1960(a19.getA1960());
        p.setA1965(a19.getA1965());
        p.setA1970(a19.getA1970());
        p.setPersonid(a19.getPersonid());
        p.setRemark(a19.getRemark());
        p.setSort(a19.getSort());
        p.setSerial(a19.getSerial());
        p.setFlag(a19.getA1935());
        return p;
    }

    @PostMapping({"/updateList/{batchId}"})
    @Transactional
    @ApiOperation("上报名单管理---flag==0保存暂不提交，flag==1保存提交")
    public R updateList(@PathVariable("batchId") String batchId, @RequestBody ConfernamelistEntity[] confernamelistEntity_arr, Integer flag) {
        for (ConfernamelistEntity c : confernamelistEntity_arr) {
            c.setSubmissdept(SecurityUtils.getDeptId());
            if (flag == null || flag.intValue() != 1) {
                c.setResult(ConferenceStateEnum.NOT_SUBMITED.getValue());
            } else {
                c.setResult(ConferenceStateEnum.SUBMITED.getValue());
            }
            this.confernamelistService.updateById(c);
        }
        return R.ok();
    }

    @RequestMapping({"/update"})
    public R update(@RequestBody ConfernamelistEntity confernamelist) {
        this.confernamelistService.updateById(confernamelist);
        return R.ok();
    }

    @Transactional
    @GetMapping({"/batchupdate"})
    @ApiOperation("批量更新批次名单表")
    public R batchupdate(@RequestBody ConfernamelistEntity[] confernamelistEntity_arr, String batchId) {
        for (ConfernamelistEntity entity : confernamelistEntity_arr) {
            this.confernamelistService.updateById(entity);
        }
        BatchconferenceEntity batchconference = (BatchconferenceEntity) this.batchconferenceService.getBaseMapper().selectById(batchId);
        batchconference.setStata(ConferenceStateEnum.TO_BE_ATTENDED.getValue());
        this.batchconferenceService.updateById(batchconference);
        return R.ok();
    }

    @GetMapping({"/returnNameList"})
    @ApiOperation("退回")
    public R returnNameList(String batchId, String dept) {
        List<ConfernamelistEntity> list = this.confernamelistService.getBaseMapper().selectList(
                new LambdaQueryWrapper<ConfernamelistEntity>().eq(ConfernamelistEntity::getBatchid, batchId).eq(ConfernamelistEntity::getSubmissdept, dept));
        list.forEach(t -> {
            t.setResult(ConferenceStateEnum.NOT_SUBMITED.getValue());
        });
        for (ConfernamelistEntity entity : list) {
            this.confernamelistService.updateById(entity);
        }
        return R.ok();
    }

    @GetMapping({"/tabmenu"})
    @ApiOperation("干部处室上人员上报动态标签卡")
    public R tabmenu(String batchid) {
        return R.ok().put("data", (Object) this.confernamelistService.tabmenu(batchid));
    }

    @PostMapping({"/delete"})
    @ApiOperation("上报名单管理---删除人员")
    public R delete(@RequestBody String[] flowids) {
        for (String str : flowids) {
            ConfernamelistEntity v = (ConfernamelistEntity) this.confernamelistService.getBaseMapper().selectById(str);
            this.postchangeService.getBaseMapper().delete(
                    new LambdaQueryWrapper<PostchangeEntity>().eq(PostchangeEntity::getNamelistid, v.getBatchid()).eq(PostchangeEntity::getPersonid, v.getPersonid()));
        }
        this.confernamelistService.getBaseMapper().deleteBatchIds(Arrays.asList(flowids));
        return R.ok();
    }

    @PostMapping({"recall/{batchId}"})
    @ApiOperation("撤回")
    public R recall(@PathVariable String batchId) {
        List<ConfernamelistEntity> list = this.confernamelistService.getConfernamelist(batchId, SecurityUtils.getDeptId());
        for (int i = 0; i < list.size(); i++) {
            ConfernamelistEntity c = list.get(i);
            c.setResult(ConferenceStateEnum.NOT_SUBMITED.getValue());
            this.confernamelistService.getBaseMapper().updateById(c);
        }
        return R.ok();
    }

    @GetMapping({"addSubmit"})
    @ApiOperation("添加人员---确定--返回页面数据查询")
    public R addSubmit(@RequestBody String[] personids) {
        //（已调整完毕） todo: 将sql迁移到xml文件中
        return R.ok().put("list", (Object) this.a01Service.getBaseMapper().selectList(
                new LambdaQueryWrapper<A01>().select(A01::getA0101,A01::getA0183,A01::getA0185,A01::getA0180,
                        A01::getA0106,A01::getA0108,A01::getA0136,A01::getA0184,A01::getA0187,A01::getA0188,
                        A01::getA0190,A01::getA0191).in(A01::getFlowid, personids)));
    }

    @PostMapping({"/personSort"})
    @ApiOperation("名单表排序")
    public R personSort(@RequestBody ConfernamelistEntity[] confernamelistEntity_arr) {
        for (ConfernamelistEntity entity : confernamelistEntity_arr) {
            this.confernamelistService.updateById(entity);
        }
        return R.ok();
    }

    @RequestMapping({"/updateByIdPass"})
    @ApiOperation("修改Confernamelist通过")
    public R updateByIdPass(String[] ids) {
        if (ids.length == 0) {
            return R.error();
        }
        for (String str : ids) {
            ConfernamelistEntity confernamelistEntity = (ConfernamelistEntity) this.confernamelistService.getBaseMapper().selectById(str);
            confernamelistEntity.setResult("通过");
            this.confernamelistService.updateByPrimaryKeySelective(confernamelistEntity);
        }
        return R.ok();
    }

    @PostMapping({"/updateByIdNoPass"})
    @ApiOperation("修改Confernamelist不通过")
    public R updateByIdNoPass(String[] ids) {
        if (ids.length == 0) {
            return R.error();
        }
        for (String str : ids) {
            ConfernamelistEntity confernamelistEntity = (ConfernamelistEntity) this.confernamelistService.getBaseMapper().selectById(str);
            confernamelistEntity.setResult("不通过");
            this.confernamelistService.updateByPrimaryKeySelective(confernamelistEntity);
        }
        return R.ok();
    }

    @PostMapping({"/saveAndUpdateA19Datas"})
    @ApiOperation("保存跟新a19表拟任拟免信息")
    public R saveAndUpdateA19Datas(String batchName) {
        if (batchName == null || batchName == "") {
            return R.error();
        }
        List<BatchconferenceEntity> batchList = this.batchconferenceService.selectByMaps(new HashMap<>());
        String batchid = "";
        if (batchList.size() > 0) {
            batchid = batchList.get(0).getFlowid();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", "通过");
        map.put("batchid", batchid);
        List<ConfernamelistEntity> list = this.confernamelistService.selectByMaps(map);
        if (list.size() <= 0) {
            return R.error();
        }
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("personid", list.get(i).getPersonid().toString());
            map1.put("namelistid", batchid);
            List<PostchangeEntity> postchangeEntities = this.postchangeService.selectByMaps(map1);
            if (postchangeEntities.size() > 0) {
                for (int j = 0; j < postchangeEntities.size(); j++) {
                    A19 a19 = new A19();
                    a19.setFlowid(postchangeEntities.get(j).getA19flowid());
                    a19.setA1905(postchangeEntities.get(j).getA1905());
                    a19.setA1910(postchangeEntities.get(j).getA1910());
                    a19.setA1915(postchangeEntities.get(j).getA1915());
                    a19.setA1916(postchangeEntities.get(j).getA1916());
                    a19.setA1920(postchangeEntities.get(j).getA1920());
                    a19.setA1925(postchangeEntities.get(j).getA1925());
                    a19.setA1930(postchangeEntities.get(j).getA1930());
                    a19.setA1935(postchangeEntities.get(j).getA1935());
                    a19.setA1940(postchangeEntities.get(j).getA1940());
                    a19.setA1945(postchangeEntities.get(j).getA1945());
                    a19.setA1950(postchangeEntities.get(j).getA1950());
                    a19.setA1955(postchangeEntities.get(j).getA1955());
                    a19.setA1956(postchangeEntities.get(j).getA1956());
                    a19.setA1960(postchangeEntities.get(j).getA1960());
                    a19.setA1965(postchangeEntities.get(j).getA1965());
                    a19.setA1970(postchangeEntities.get(j).getA1970());
                    a19.setPersonid(postchangeEntities.get(j).getPersonid());
                    a19.setRemark(postchangeEntities.get(j).getRemark());
                    a19.setSerial(postchangeEntities.get(j).getSerial());
                    a19.setSort(postchangeEntities.get(j).getSort());
                    if (postchangeEntities.get(j).getFlag().shortValue() == 0) {
                        a19.setFlowid(GenerateUUID.getUUID());
                        this.a19Service.getBaseMapper().insert(a19);
                    } else {
                        this.a19Service.updateByPrimaryKeySelective(a19);
                    }
                }
            }
        }
        return R.ok();
    }

    @GetMapping({"/getUpMeetList"})
    @ApiOperation("上会列表")
    public R getUpMeetList(String batchid) {
        if (batchid == null || batchid == "") {
            return R.error();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("batchid", batchid);
        return R.ok().put("data", (Object) this.confernamelistService.getUpMeetList(map));
    }
}
